package com.cyou.dream.define;

import com.cyou.dream.core.IRule;
import com.cyou.dream.core.ScriptRegistry;
import com.cyou.dream.rule.*;
import lombok.Getter;
import org.springframework.data.redis.core.script.RedisScript;

import java.util.function.Supplier;

/**
 * 规则枚举类，定义规则、规则脚本、规则处理器
 */
@Getter
public enum ERule {

    /**
     * 时间窗口计数器规则
     */
    TIME_WINDOW_COUNTER(getRuleScript("time_window_counter.lua"), TimeWindowCounterRule::new),

    /**
     * 自然日计数器规则
     */
    NATURE_DAY_COUNTER(getRuleScript("nature_day_counter.lua"), NaturalDayCounterRule::new),

    /**
     * 自增计数器规则（一次+1）
     */
    INCREMENT_COUNTER(getRuleScript("increment_counter.lua"), IncrementCounterRule::new),

    /**
     * 多步长计数器规则（一次+n）
     */
    MULTI_STEP_COUNTER(getRuleScript("multi_step_counter.lua"), MultiStepCounterRule::new),

    /**
     * 虚拟手机号合法性验证规则
     */
    VIRTUAL_PHONE(null, VirtualPhoneRule::new)

    ;

    private final RedisScript<?> ruleScript;

    private final Supplier<IRule> ruleSupplier;

    ERule(RedisScript<?> ruleScript, Supplier<IRule> ruleSupplier) {
        this.ruleScript = ruleScript;
        this.ruleSupplier = ruleSupplier;
    }

    private static RedisScript<?> getRuleScript(String ruleScript) {
        return ScriptRegistry.getScript(ruleScript);
    }
}
